﻿Public Class TB_GENERICA
    Dim Ct As Integer = 0
    Dim CTP As Integer = 0

    Dim CsqlInsertCampos As String
    Dim CsqlInsertValores As String

    Dim CsqlUpdate As String
    Dim CsqlUpdateWhere As String

    Public Sub Save(ByRef Rp As DotNetResponse.SQLPersistence, ByRef Pan As Object, ByVal PkValue As String, ByVal PkName As String, Optional ByVal GetIdentity As Boolean = False)
        If PkValue = "0" Then

            'cnStr = "Provider=SQLOLEDB;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=FACTUA;Data Source=PC10\SWFACTURA;"
            'cn.ConnectionString = cnStr
            'Sql = "INSERT INTO usuario(nombres,usuario,clave,confclave) VALUES(@nom, @nom, @nom, @nom)"
            'cmd = New OleDbCommand(Sql, cn)
            'cmd.Parameters.Add(New OleDb.OleDbParameter("@nom", OleDbType.VarChar))
            'cmd.Parameters("@nom").Value = TextBox1.Text
            'cmd.Connection.Open()
            'cmd.ExecuteReader()
            'cn.Close()

            CsqlInsertCampos = " INSERT INTO dbo." & Pan.SkinID & " ( "
            CsqlInsertValores = " values ( "
            InsertRecursivo(Pan)

            CsqlInsertCampos = CsqlInsertCampos & " )"
            CsqlInsertValores = CsqlInsertValores & " )"

            Dim Rpexec As New DotNetResponse.SQLPersistence
            Dim Exec As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
            If GetIdentity Then
                Exec.ExecuteDataTable(Rp, CsqlInsertCampos & " " & CsqlInsertValores & " " & IIf(GetIdentity, " select @@IDENTITY ", ""))
            Else
                Exec.ExecuteNonQuery(Rp, CsqlInsertCampos & " " & CsqlInsertValores)
            End If

            Exec.Commit()
            Exec.Dispose()



        Else
            CsqlUpdate = " update dbo." & Pan.SkinID & "  SET "

            UpdateRecursivo(Pan)

            'Dim Rpexec As New DotNetResponse.SQLPersistence
            Dim Exec As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
            Exec.ExecuteNonQuery(Rp, CsqlUpdate & " " & CsqlUpdateWhere)
            Exec.Commit()
            Exec.Dispose()

        End If

    End Sub
    Sub InsertRecursivo(ByRef Obj As Object)
        If Obj.controls.count > 0 Then
            Dim i As Integer
            For i = 0 To Obj.controls.count - 1
                InsertRecursivo(Obj.controls(i))
            Next
        Else
            If InStr(Obj.SkinID, "D_AUD_FECHA_C") > 0 Then
                Obj.text = Format(Now, "yyyyMMdd hhmm")
            End If
            If InStr(Obj.SkinID, "D_AUD_FECHA_M") > 0 Then
                Obj.text = Format(Now, "yyyyMMdd hhmm")
            End If

            If Obj.GetType.ToString = "Anthem.Label" Then
                If DirectCast(Obj, Anthem.Label).SkinID.Trim.Length > 0 Then
                    AddInsert(DirectCast(Obj, Anthem.Label).SkinID.Trim, DirectCast(Obj, Anthem.Label).Text)
                End If
            End If

            If Obj.GetType.ToString = "Anthem.TextBox" Then
                If DirectCast(Obj, Anthem.TextBox).SkinID.Trim.Length > 0 Then
                    AddInsert(DirectCast(Obj, Anthem.TextBox).SkinID.Trim, DirectCast(Obj, Anthem.TextBox).Text)
                End If
            End If

            If Obj.GetType.ToString = "Anthem.DropDownList" Then
                If DirectCast(Obj, Anthem.DropDownList).SkinID.Trim.Length > 0 Then
                    AddInsert(DirectCast(Obj, Anthem.DropDownList).SkinID.Trim, DirectCast(Obj, Anthem.DropDownList).SelectedValue)
                End If
            End If

        End If
    End Sub
    Sub AddInsert(ByVal NombreCampo As String, ByVal Valor As String)
        If Mid(NombreCampo, 1, 1).ToUpper = "P" Then
            Exit Sub
        End If

        Dim Cremilla As String = "'"
        Select Case Mid(NombreCampo, 1, 1)
            Case "I"
                Cremilla = ""
        End Select

        If Ct = 0 Then
            Ct = 1
            CsqlInsertCampos = CsqlInsertCampos & NombreCampo
            CsqlInsertValores = CsqlInsertValores & Cremilla & Valor.Trim & Cremilla

        Else
            CsqlInsertCampos = CsqlInsertCampos & "," & NombreCampo
            CsqlInsertValores = CsqlInsertValores & "," & Cremilla & Valor.Trim & Cremilla

        End If
    End Sub
    Sub UpdateRecursivo(ByRef Obj As Object)
        If Obj.controls.count > 0 Then
            Dim i As Integer
            For i = 0 To Obj.controls.count - 1
                UpdateRecursivo(Obj.controls(i))
            Next
        Else
            If InStr(Obj.SkinID, "D_AUD_FECHA_M") > 0 Then
                Obj.text = Format(Now, "yyyyMMdd hhmm")
            End If

            If Obj.GetType.ToString = "Anthem.Label" Then
                If DirectCast(Obj, Anthem.Label).SkinID.Trim.Length > 0 Then
                    AddUpdate(DirectCast(Obj, Anthem.Label).SkinID.Trim, DirectCast(Obj, Anthem.Label).Text)
                End If
            End If

            If Obj.GetType.ToString = "Anthem.TextBox" Then
                If DirectCast(Obj, Anthem.TextBox).SkinID.Trim.Length > 0 Then
                    AddUpdate(DirectCast(Obj, Anthem.TextBox).SkinID.Trim, DirectCast(Obj, Anthem.TextBox).Text)
                End If
            End If

            If Obj.GetType.ToString = "Anthem.DropDownList" Then
                If DirectCast(Obj, Anthem.DropDownList).SkinID.Trim.Length > 0 Then
                    AddUpdate(DirectCast(Obj, Anthem.DropDownList).SkinID.Trim, DirectCast(Obj, Anthem.DropDownList).SelectedValue)
                End If
            End If

        End If
    End Sub
    Sub AddUpdate(ByVal NombreCampo As String, ByVal Valor As String)
        If Mid(NombreCampo, 1, 1) = "P" Then
            If CTP = 0 Then
                CTP = 1
                CsqlUpdateWhere = " where " & NombreCampo & " = " & Valor.Trim
            Else
                CsqlUpdateWhere = " and " & NombreCampo & " = " & Valor.Trim
            End If

            Exit Sub
        End If

        Dim Cremilla As String = "'"
        Select Case Mid(NombreCampo, 1, 1)
            Case "I"
                Cremilla = ""
        End Select


        If Ct = 0 Then
            Ct = 1
            CsqlUpdate = CsqlUpdate & NombreCampo & "=" & Cremilla & Valor.Trim & Cremilla
        Else
            CsqlUpdate = CsqlUpdate & "," & NombreCampo & "=" & Cremilla & Valor.Trim & Cremilla
        End If
    End Sub

    Sub CargarGrilla(ByRef Rp As DotNetResponse.SQLPersistence, ByRef Grid As Anthem.GridView, ByVal Csql As String)

        Dim Rpexec As New DotNetResponse.SQLPersistence
        Dim Exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Exec.ExecuteDataTable(Rpexec, Csql)
        Exec.Dispose()

        If Rpexec.TieneDatos Then
            Grid.DataSource = Rpexec.DtTable
            Grid.DataBind()
            Rp.DtTable = Rpexec.DtTable
            Rp.TieneDatos = Rpexec.TieneDatos
        End If


    End Sub
    Sub CargarGrilla(ByRef Rp As DotNetResponse.SQLPersistence, ByRef Grid As Anthem.DataGrid, ByVal Csql As String)

        Dim Rpexec As New DotNetResponse.SQLPersistence
        Dim Exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Exec.ExecuteDataTable(Rpexec, Csql)
        Exec.Dispose()

        If Rpexec.TieneDatos Then
            Grid.DataSource = Rpexec.DtTable
            Grid.DataBind()
            Rp.DtTable = Rpexec.DtTable
            Rp.TieneDatos = Rpexec.TieneDatos
        End If


    End Sub
    Sub GetDataTable(ByRef Rp As DotNetResponse.SQLPersistence, ByVal Csql As String)

        Dim Rpexec As New DotNetResponse.SQLPersistence
        Dim Exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Exec.ExecuteDataTable(Rpexec, Csql)
        Exec.Dispose()

        If Rpexec.TieneDatos Then
            Rp.DtTable = Rpexec.DtTable
            Rp.TieneDatos = Rpexec.TieneDatos
        End If


    End Sub

    Sub ClearObject(ByRef Obj As Object)
        If Obj.controls.count > 0 Then
            Dim i As Integer
            For i = 0 To Obj.controls.count - 1
                ClearObject(Obj.controls(i))
            Next
        Else
            If Obj.GetType.ToString = "Anthem.TextBox" Then
                If DirectCast(Obj, Anthem.TextBox).SkinID.Trim.Length > 0 Then
                    DirectCast(Obj, Anthem.TextBox).Text = ""
                    DirectCast(Obj, Anthem.TextBox).UpdateAfterCallBack = True

                End If
            End If

            If Obj.GetType.ToString = "Anthem.DropDownList" Then
                DirectCast(Obj, Anthem.DropDownList).SelectedIndex = 0
                DirectCast(Obj, Anthem.DropDownList).UpdateAfterCallBack = True
            End If
        End If

    End Sub

End Class
